﻿using System;
using System.ComponentModel;

namespace Microsoft.Practices.CompositeUI.EventBroker
{
    /// <summary>
    /// 	Declares a method as an <see cref = "EventTopic" /> subscription.
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class EventSubscriptionAttribute : Attribute
    {
        #region Fields & Properties

        private readonly string _topic;

        /// <summary>
        /// 	The name of the <see cref = "EventTopic" /> the decorated method is subscribed to.
        /// </summary>
        public string Topic
        {
            get
            {
                return _topic;
            }
        }

        private ThreadOption _threadOption;

        /// <summary>
        /// 	Indicates the way the subscription method should be called, see
        /// 	<see cref = "ThreadOption" /> enumeration for the supported modes.
        /// </summary>
        [DefaultValue(ThreadOption.Publisher)]
        public ThreadOption Thread
        {
            get
            {
                return _threadOption;
            }
            set
            {
                _threadOption = value;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// 	Declares a method as an <see cref = "EventTopic" /> subscription.
        /// </summary>
        /// <param name = "topic">The name of the <see cref = "EventTopic" /> to subscribe to.</param>
        public EventSubscriptionAttribute(string topic) : this(topic, ThreadOption.Publisher)
        {
        }

        /// <summary>
        /// 	Delcares a method as an <see cref = "EventTopic" /> subscription using the specified <see cref = "ThreadOption" />.
        /// </summary>
        /// <param name = "topic">The name of the <see cref = "EventTopic" /> to subscribe to.</param>
        /// <param name = "threadOption">The <see cref = "ThreadOption" /> indicating how the method should be called.</param>
        public EventSubscriptionAttribute(string topic, ThreadOption threadOption)
        {
            _topic = topic;
            _threadOption = threadOption;
        }

        #endregion
    }
}